package com.itheima.web.listener;

import com.itheima.autoconfig.AliyunGreenTemplate;
import com.itheima.domain.mongo.Movement;
import com.itheima.service.mongo.MovementService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.bson.types.ObjectId;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class MovementStateListener {

    @DubboReference
    private MovementService movementService;

    @Autowired
    private AliyunGreenTemplate aliyunGreenTemplate;

    @RabbitListener(queuesToDeclare = @Queue("tanhua.movement.log"))
    public void listenMovementState(String movementId){
        System.out.println("动态审核start...");
        // 查询动态详情
        Movement movement = movementService.findById(new ObjectId(movementId));

        // 审核图片和文本
        Boolean checkText = aliyunGreenTemplate.checkText(movement.getTextContent());
        Boolean checkImage = aliyunGreenTemplate.checkImage(movement.getMedias());
        if (checkText&&checkImage) {
            movement.setState(1); // 通过
        }else{
            movement.setState(2); // 拒绝，后面人工复审
        }

        // 更新mongo
        movementService.update(movement);
        System.out.println("动态审核end...");
    }
}
